﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dcms.BLL;
using Dcms.Model;
using AjaxPro;
using System.Collections.Generic;

public partial class Manage_Modle_CateGory_CatesList_1 : System.Web.UI.Page
{
    public CompetenceLib CL = new CompetenceLib();
    public String Language = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        CL.IsLogin();
        if (Session["DCMSLANGUAGE"] != null)
        {
            Language = Session["DCMSLANGUAGE"].ToString();
        }
        Utility.RegisterTypeForAjax(typeof(Dcms.BLL.Dcms_CateBLL));
        if (!IsPostBack)
        {
            LoadLanguage();
        }
    }

    /// <summary>
    /// 加载
    /// </summary>
    private void LoadLanguage()
    {
        //取得所有语言版本
        string strLanguage = ConfigurationManager.AppSettings["Language"];
        string strLanguageNmae = ConfigurationManager.AppSettings["LanguageNmae"];
        string[] arrLanguage = strLanguage.Split(',');
        string[] arrLanguageNmae = strLanguageNmae.Split(',');
        for (int i = 0; i < arrLanguage.Length; i++)
        {
            if (arrLanguage[i].Trim().ToLower() != Language.Trim().ToLower())
            {
                ddlLanguage.Items.Add(new ListItem(arrLanguageNmae[i], arrLanguage[i]));
            }
        }
        ddlLanguage.Items.Insert(0, new ListItem("请选择语言", "0"));
        ddlLanguage.SelectedIndex = 0;
    }

    private string strOldCateIDs = "";
    private string strNewIDs = "";
    /// <summary>
    /// 复制到其它语言版本
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCopy_Click(object sender, EventArgs e)
    {
        try
        {
            if (ddlLanguage.SelectedIndex > 0)
            {
                Dcms_CateBLL cBll = new Dcms_CateBLL();
                cBll.DeleteModelListByLanguage(ddlLanguage.SelectedValue);
                IList<Dcms_CateModel> lDcms_CateModel = cBll.GetModelListByLanguage(Session["DCMSLANGUAGE"].ToString());
                strOldCateIDs = "";
                strNewIDs = "";
                foreach (Dcms_CateModel etDcms_CateModel in lDcms_CateModel)
                {
                    Dcms_CateModel etNewDcms_CateModel = new Dcms_CateModel();
                    etNewDcms_CateModel.Cate_AddTime = etDcms_CateModel.Cate_AddTime;
                    etNewDcms_CateModel.Cate_Depth = etDcms_CateModel.Cate_Depth;
                    etNewDcms_CateModel.Cate_Flag = etDcms_CateModel.Cate_Flag;
                    etNewDcms_CateModel.Cate_Image = etDcms_CateModel.Cate_Image;
                    etNewDcms_CateModel.Cate_Intro = etDcms_CateModel.Cate_Intro;
                    etNewDcms_CateModel.Cate_IsChild = etDcms_CateModel.Cate_IsChild;
                    etNewDcms_CateModel.Cate_IsCommentFlag = etDcms_CateModel.Cate_IsCommentFlag;
                    etNewDcms_CateModel.Cate_IsRevertFlag = etDcms_CateModel.Cate_IsRevertFlag;
                    etNewDcms_CateModel.Cate_Key = etDcms_CateModel.Cate_Key;
                    etNewDcms_CateModel.Cate_Lang = ddlLanguage.SelectedValue;
                    etNewDcms_CateModel.Cate_ModelKeyId = etDcms_CateModel.Cate_ModelKeyId;
                    etNewDcms_CateModel.Cate_Name = etDcms_CateModel.Cate_Name;
                    etNewDcms_CateModel.Cate_OpType = etDcms_CateModel.Cate_OpType;
                    etNewDcms_CateModel.Cate_Order = etDcms_CateModel.Cate_Order;
                    ///新加
                    etNewDcms_CateModel.Cate_ViewURL = etDcms_CateModel.Cate_ViewURL;
                    etNewDcms_CateModel.Cate_Guid = System.Guid.NewGuid().ToString("N");

                    etNewDcms_CateModel.Cate_browse = etDcms_CateModel.Cate_browse;
                    etNewDcms_CateModel.Cate_auditing = etDcms_CateModel.Cate_auditing;
                    etNewDcms_CateModel.Cate_IsCapability = etDcms_CateModel.Cate_IsCapability;
                    etNewDcms_CateModel.Cate_showtype = etDcms_CateModel.Cate_showtype;
                    etNewDcms_CateModel.Cate_magstate = etDcms_CateModel.Cate_magstate;
                    etNewDcms_CateModel.Cate_BackCate = etDcms_CateModel.Cate_BackCate;
                    etNewDcms_CateModel.Cate_Layer = etDcms_CateModel.Cate_Layer;
                    etNewDcms_CateModel.Cate_AllAction = etDcms_CateModel.Cate_AllAction;
                    // etNewDcms_CateModel.Cate_Layer = etDcms_CateModel.Cate_Layer;
                    // etNewDcms_CateModel.Cate_AllAction = etDcms_CateModel.Cate_AllAction;
                    ////
                    int intParentID = getParentID(etDcms_CateModel.Cate_ParentID);
                    etNewDcms_CateModel.Cate_ParentID = intParentID;
                    if (intParentID == 0)
                    {
                        etNewDcms_CateModel.Cate_IDPath = "";
                    }
                    else
                    {
                        etNewDcms_CateModel.Cate_IDPath = cBll.GetModelByID(intParentID).Cate_IDPath;
                    }
                    etNewDcms_CateModel.Cate_State = etDcms_CateModel.Cate_State;
                    etNewDcms_CateModel.Cate_Url = etDcms_CateModel.Cate_Url;
                    cBll.AddModel(etNewDcms_CateModel);
                    strOldCateIDs += (strOldCateIDs == "" ? etDcms_CateModel.Cate_ID.ToString() : "," + etDcms_CateModel.Cate_ID);
                    strNewIDs += (strNewIDs == "" ? etNewDcms_CateModel.Cate_ID.ToString() : "," + etNewDcms_CateModel.Cate_ID);
                }
                RegisterStartupScript(this.ToString(), "<script>alert('复制所有分类完成!');</script>");
            }
        }
        catch (Exception ex)
        {
            RegisterStartupScript(this.ToString(), "<script>alert('" + ex.Message + "');</script>");
        }
    }

    private int getParentID(int oldCateID)
    {
        string sOldCateID = oldCateID.ToString();
        if (oldCateID == 0)
        {
            return 0;
        }
        string[] arrOldCateIDs = strOldCateIDs.Split(',');
        string[] arrNewCateIDs = strNewIDs.Split(',');
        for (int i = 0; i < arrOldCateIDs.Length; i++)
        {
            if (arrOldCateIDs[i].Trim() == sOldCateID)
            {
                return Convert.ToInt32(arrNewCateIDs[i]);
            }
        }
        return -1;
    }
}
